function getOutStr()
{
	var level2str = JSON.stringify(treebar.level2);
	var level3str = JSON.stringify(treebar.level3);
	var level4str = JSON.stringify(treebar.level4);
	var level5str = JSON.stringify(treebar.level5);
	outline = '[';
    for (i = 0; i < (treebar.level1.length); i++) {
        outline += '{';
        outline += '"text":"' + treebar.level1[i].text + '",';
		if(treebar.level1[i].iconcls!='')
			outline += '"iconCls":"' + treebar.level1[i].iconcls + '",';
		if(((level2str.split(treebar.level1[i].selfid)).length-1)>0)
			outline += '"state":"' + treebar.level1[i].state + '",';
		else
			outline += '"id":' + treebar.level1[i].selfid + ',';
        outline += '"checked":' + treebar.level1[i].checked + ',';
        outline += '"children":[';
		if(((level2str.split(treebar.level1[i].selfid)).length-1)>0){
			for (j = 0; j < (treebar.level2.length); j++) {
				if(treebar.level1[i].selfid == treebar.level2[j].parentid){
					outline += '{';
					outline += '"text":"' + treebar.level2[j].text + '",';
					if(treebar.level2[j].iconcls!='')
						outline += '"iconCls":"' + treebar.level2[j].iconcls + '",';
					if(((level3str.split(treebar.level2[j].selfid)).length-1)>0)
						outline += '"state":"' + treebar.level2[j].state + '",';
					else
						outline += '"id":' + treebar.level2[j].selfid + ',';
					outline += '"checked":' + treebar.level2[j].checked + ',';
					outline += '"children":[';
					if(((level3str.split(treebar.level2[j].selfid)).length-1)>0){
						for (k = 0; k < (treebar.level3.length); k++) {
							if(treebar.level2[j].selfid ==  treebar.level3[k].parentid){
								outline += '{';
								outline += '"text":"' + treebar.level3[k].text + '",';
								if(treebar.level3[k].iconcls!='')
									outline += '"iconCls":"' + treebar.level3[k].iconcls + '",';
								if(((level4str.split(treebar.level3[k].selfid)).length-1)>0)
									outline += '"state":"' + treebar.level3[k].state + '",';
								else
									outline += '"id":' + treebar.level3[k].selfid + ',';
								outline += '"checked":' + treebar.level3[k].checked + ',';
								outline += '"children":[';
								if(((level4str.split(treebar.level3[k].selfid)).length-1)>0){
									for (m = 0; m < (treebar.level4.length); m++) {
										if(treebar.level3[k].selfid ==  treebar.level4[m].parentid){
											outline += '{';
											outline += '"text":"' + treebar.level4[m].text + '",';
											if(treebar.level4[m].iconcls!='')
												outline += '"iconCls":"' + treebar.level4[m].iconcls + '",';
											if(((level5str.split(treebar.level4[m].selfid)).length-1)>0)
												outline += '"state":"' + treebar.level4[m].state + '",';
											else
												outline += '"id":' + treebar.level4[m].selfid + ',';
											outline += '"checked":' + treebar.level4[m].checked + ',';
											outline += '"children":[';
											if(((level5str.split(treebar.level4[m].selfid)).length-1)>0){
												for (n = 0; n < (treebar.level5.length); n++) {
													if(treebar.level4[m].selfid ==  treebar.level5[n].parentid){
														outline += '{"id":' + treebar.level5[n].selfid + ',';
														outline += '"text":"' + treebar.level5[n].text + '",';
														if(treebar.level5[n].iconcls!='')
															outline += '"iconCls":"' + treebar.level5[n].iconcls + '",';
														outline += '"checked":' + treebar.level5[n].checked + '},';
													}								
												}
											}
											outline+=']},';
										}						
									}	
								}
								outline+=']},';
							}
						}
					}
					outline+=']},';
				}
			}
		}
        outline += ']},';    
    }
    outline += ']';
	//console.log(outline.replace(/,]/g, "]"));
	return outline.replace(/,]/g, "]")

}

function setparams(selfid,parentid,text,iconcls,state,checked) {
    this.selfid = selfid;
    this.parentid = parentid;
    this.text = text;
	if(iconcls != undefined && iconcls != null && iconcls != '')
		this.iconcls = iconcls;
	else
		this.iconcls = '';
	if(state != undefined && state != null && state != '')
		this.state = state;
	else
		this.state = 'closed';
	if(checked != undefined && checked != null && checked != '')
		this.checked = checked;
	else
		this.checked = 'false';
}

function addlevel(grade,selfid,parentid,text,iconcls,state,checked) {
    if(grade==1){
		treebar.level1[treebar.level1.length] = new setparams(selfid,0,text,iconcls,state,checked);
	}else{
		switch(grade){
			case 2:
				if (parent != null) 
					treebar.level2[treebar.level2.length] = new setparams(selfid,parentid,text,iconcls,state,checked);
				break;
			case 3:
				if (parent != null) 
					treebar.level3[treebar.level3.length] = new setparams(selfid,parentid,text,iconcls,state,checked);		
				break;
			case 4:
				if (parent != null) 
					treebar.level4[treebar.level4.length] = new setparams(selfid,parentid,text,iconcls,state,checked);		
				break;
			case 5:
				if (parent != null) 
					treebar.level5[treebar.level5.length] = new setparams(selfid,parentid,text,iconcls,state,checked);		
				break;
		}
	}
}

function outlook() {
	this.level1 = new Array();
    this.level2 = new Array();
	this.level3 = new Array();
	this.level4 = new Array();
	this.level5 = new Array();
	this.addlevel = addlevel;
	this.getOutStr=getOutStr;	
}

var treebar = new outlook();
